import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.*;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;
import org.mybatis.generator.utils.FileUtils;

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class GeneratorMain {

    private static final String PROFILE = "mybatis/generatorConfig-research.xml";
    private static final String TRADE_BI = "mybatis/generatorConfig-trade-bi.xml";

    private static final String CONF_FILE_NAME = TRADE_BI;

    public static void main(String[] args) throws Exception {
        deleteExistFiles();
        GeneratorMain generatorMain = new GeneratorMain();
        generatorMain.generator(args);
    }


    public void generator(String[] args) throws Exception {
        List<String> warnings = new ArrayList<>();
        ConfigurationParser cp = new ConfigurationParser(warnings);

        InputStream configFile = GeneratorMain.class.getClassLoader().getResourceAsStream(CONF_FILE_NAME);
        Configuration config = cp.parseConfiguration(configFile);
        readContextsProperties(config);
        DefaultShellCallback callback = new DefaultShellCallback(true);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        myBatisGenerator.generate(null);

    }

    private static void readContextsProperties(Configuration config) {
        for (Context context : config.getContexts()) {
            JavaModelGeneratorConfiguration javaModelConf = context.getJavaModelGeneratorConfiguration();
            SqlMapGeneratorConfiguration sqlMapConf = context.getSqlMapGeneratorConfiguration();
            JavaClientGeneratorConfiguration javaClientConf = context.getJavaClientGeneratorConfiguration();
            readProperties(context, javaModelConf, sqlMapConf, javaClientConf);
        }
    }

    /**
     * 读取定义的变量
     *
     * @param context
     * @param targetPackageConfs
     */
    public static void readProperties(Context context, TargetPackageConf... targetPackageConfs) {
        for (TargetPackageConf conf : targetPackageConfs) {
            String targetPackage = conf.getTargetPackage();
            Pattern pattern = Pattern.compile("\\$\\{([\\w.]*)}");
            Matcher matcher = pattern.matcher(targetPackage);
            if (matcher.find()) {
                String varName = matcher.group(1);
                String property = context.getProperty(varName);
                if (property == null) throw new RuntimeException(String.format("未找到变量:%s", varName));
                conf.setTargetPackage(property);
            }
        }

    }

    private static void deleteExistFiles() {
        File javaFileDir = new File("./src/out/java");
        FileUtils.emptyDir(javaFileDir);
        File mapperDir = new File("./src/out/resources");
        FileUtils.emptyDir(mapperDir);
    }

}
